<?php

class FormFilter
{

  /**
   * @var string
   */
  public $field;

  /**
   * @var string
   */
  public $foreignKey;

  /**
   * @var Form
   */
  public $form;

  /**
   * @var string
   */
  public $name;

  /**
   * @var string
   */
  public $primaryKey;

  /**
   * @var boolean
   */
  public $required = false;

  /**
   * @var string
   */
  public $title;

  /**
   * @var mixed
   */
  protected $value;

  /**
   * @var FilterChain
   */
  protected $filterChain;

  /**
   * $var IView
   */
  private $view;

  /**
   * Constructor.
   *
   * @param string $name the filter's name
   * @param string $title the filter's title
   * @return void
   */
  public function __construct($name, $title = null)
  {
    $this->name = $name;
    $this->title = $title;
  }

  public function execute()
  {
    $value = Simplify::getRequest()->get($this->getPrimaryKey());

    $this->setValue($value);

    Simplify::getResponse()->getParams()->set($this->getPrimaryKey(), $value);
  }

  public function render()
  {
    $view = $this->getView();

    $view->set('name', $this->getPrimaryKey());
    $view->set('value', $this->getValue());

    return $view->render();
  }

  /**
   * get element name
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * get element title
   */
  public function getTitle()
  {
    if (is_null($this->title)) {
      $this->title = Inflector::titleize($this->getName());
    }

    return $this->title;
  }

  /**
   * get field name
   */
  public function getField()
  {
    if (empty($this->field)) {
      $this->field = Inflector::underscore($this->getName());
    }

    return $this->field;
  }

  public function getPrimaryKey()
  {
    if (empty($this->primaryKey)) {
      $this->primaryKey = $this->form->getPk();
    }

    return $this->primaryKey;
  }

  public function getForeignKey()
  {
    if (empty($this->foreignKey)) {
      $this->foreignKey = $this->getField();
    }

    return $this->foreignKey;
  }

  public function setDataFromPost(&$row)
  {
    $this->setRowValue($row, $this->getValue());
  }

  public function getValue()
  {
    return $this->value;
  }

  public function setValue($value)
  {
    $this->value = $value;
  }

  protected function getDisplayValue(&$row)
  {
    return $this->getValue($row);
  }

  protected function getRowValue(&$row)
  {
    return $row[$this->getField()];
  }

  protected function setRowValue(&$row)
  {
    $row[$this->getField()] = $this->value;
  }

  protected function getFieldValue(&$row, $name)
  {
    return $row[$name];
  }

  protected function getView($type = 'PHP')
  {
    if (empty($this->view)) {
      $this->view = View::factory($type);

      $this->view->set('action', $action);
      $this->view->set('name', $this->name);
      $this->view->set('title', $this->title);

      $this->view->setTemplate($this->getTemplate());
    }

    return $this->view;
  }

  protected function getTemplate()
  {
    return dirname(__file__) . '/view/form_filter_view.php';
  }

  /**
   *
   * @return FilterChain
   */
  public function filters($hook = null)
  {
    if (empty($this->filterChain)) {
      $this->filterChain = new FilterChain();
    }

    if (func_num_args()) {
      return call_user_func_array(array($this->filterChain, 'call'), func_get_args());
    }

    return $this->filterChain;
  }

}

?>